package com.shuoyue.ycgk.ui.questionbank.note;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.QuestionNote;
import com.shuoyue.ycgk.ui.common.NoteContract;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import butterknife.BindView;
import butterknife.OnClick;

public class NoteListActivity extends BaseMvpActivity<NoteContract.Presenter> implements NoteContract.View {
    ListWithPage listWithPage;

    int questionId = -1;
    NoteAdapter noteAdapter;

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

//    public static void start(Context sourse, int questionId) {
//        sourse.startActivity(new Intent(sourse, NoteListActivity.class).putExtra("id", questionId));
//    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_note_list;
    }

    @Override
    protected void initView() {
        pageTitle.setText("全部笔记");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("写笔记");
        tvRight.setTextColor(Color.parseColor("#1C90F5"));
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));
        noteAdapter = new NoteAdapter(null);
        recyclerView.setAdapter(noteAdapter);
        refreshlayout.setEnableLoadMore(false);

        refreshlayout.setOnRefreshListener(refreshLayout -> {
            listWithPage = null;
            mPresenter.getQuestionNotes(questionId, 1);
            refreshLayout.finishRefresh(2000);
        });
        refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getQuestionNotes(questionId, listWithPage.getCurrent() + 1);
            refreshLayout.finishLoadMore(2000);
        });
        noteAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            QuestionNote note = (QuestionNote) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.lay_praise:
                    mPresenter.praiseNote(note, note.getIsLike() == 1 ? 0 : 1);
                    break;
            }
        });
        registEmptyView(noteAdapter);
    }

    @Override
    public void initData() {
        questionId = getIntent().getIntExtra("id", -1);
        mPresenter = new NoteContract.Presenter();
        mPresenter.attachView(this);
    }

    @Override
    public void setNotes(ListWithPage<QuestionNote> list) {
        listWithPage = list;
        if (list.getCurrent() == 1) {
            noteAdapter.resetData(list.getRecords());
        } else {
            noteAdapter.addData(list.getRecords());
        }
        refreshlayout.setEnableLoadMore(list.getCurrent() != list.getPages()&& list.getPages() > 0);
    }

    @Override
    public void addNoteSuc() {

    }

    @Override
    public void praiseResult(QuestionNote actionId, boolean isPraised) {
        actionId.setIsLike(isPraised ? 1 : 0);
        actionId.setLikeCount(actionId.getLikeCount() + (isPraised ? 1 : -1));
        noteAdapter.notifyDataSetChanged();
    }


    @OnClick({R.id.back, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_right:
                EditNoteActivity.start(this, questionId);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getQuestionNotes(questionId, 1);
    }
}
